{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "orig_nbformat": 4,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.6 64-bit"
  },
  "interpreter": {
   "hash": "e4d85d63baa40a9854e1e91e14fde8d7f9b9a564a40f3bc91b58b47abf2bd79f"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "import jieba\r\n",
    "text = open(\"./《三国演义》.txt\", mode=\"r\", encoding=\"utf8\").read()\r\n",
    "\r\n",
    "print(len(text))\r\n",
    "\r\n",
    "cut_items = jieba.cut(text)\r\n",
    "print(\"cuted\")\r\n",
    "\r\n",
    "data = {}\r\n",
    "for k in cut_items:\r\n",
    "    if k in data:\r\n",
    "        data[k] += 1\r\n",
    "    else:\r\n",
    "        data[k] = 1\r\n",
    "\r\n",
    "\r\n",
    "print(len(data))\r\n",
    "\r\n",
    "\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "602049\n",
      "cuted\n",
      "49015\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "data2 = sorted(data.items(), key=lambda x:x[1], reverse=True)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "source": [
    "text.count(\"曹操\") + text.count('丞相') + text.count('孟德')"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1517"
      ]
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "source": [
    "text.count('刘备') + text.count('玄德')"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "2110"
      ]
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "source": [
    "text.count('诸葛') + text.count('孔明')"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "2037"
      ]
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "metadata": {}
  }
 ]
}